import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'

Vue.use(VueRouter)

// 阻止报错
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "login" */ '@/views/BlogLogin')
    // component: resolve => require(['@/views/BlogLogin.vue'], resolve)
  },
  {
    path: '/home',
    meta: { title: '主页' },
    name: 'home',
    component: () => import(/* webpackChunkName: "home" */'@/views/BlogHome'),
    children: [{
      path: 'main',
      name: 'main',
      component: () => import(/* webpackChunkName: "main" */'@/views/blogHome/HomeMain'),
      meta: { title: '首页' }
    },
    {
      path: 'admin',
      name: 'admin',
      meta: { title: '用户管理' },
      component: () => import(/* webpackChunkName: "admin" */'@/views/blogHome/HomeAdmin'),
      children: [
        {
          name: 'adminmain',
          path: 'adminmain',
          component: () => import(/* webpackChunkName: "adminmain" */'@/views/blogHome/HomeAdmin/AdminMain'),
          meta: { title: '主管理员' }
        },
        {
          name: 'else',
          path: 'else',
          component: () => import(/* webpackChunkName: "else" */'@/views/blogHome/HomeAdmin/AdminElse'),
          meta: { title: '分管理员' },
          children: [
            {
              name: 'adminSet',
              path: 'set',
              component: () => import(/* webpackChunkName: "adminSet" */'@/components/homeAdmin/SetAdmin'),
              meta: { title: '基本操作' }
            },
            {
              name: 'adminAdd',
              path: 'add',
              component: () => import(/* webpackChunkName: "adminAdd" */'@/components/homeAdmin/AddAdmin'),
              meta: { title: '添加管理' }
            },
            {
              path: '/home/admin/else',
              redirect: '/home/admin/else/set'
            }
          ]
        },
        {
          path: '/home/admin',
          redirect: '/home/admin/adminmain'
        }
      ]
    },
    {
      path: 'center',
      name: 'center',
      meta: { title: '个人中心' },
      component: () => import(/* webpackChunkName: "centerchunk" */'@/views/blogHome/HomeCenter')

    },
    {
      path: 'comment',
      name: 'comment',
      meta: { title: '评论管理' },
      component: () => import(/* webpackChunkName: "comment" */'@/views/blogHome/FrontComment')
    },
    {
      path: 'article',
      name: 'article',
      meta: { title: '文章管理' },
      component: () => import(/* webpackChunkName: "article" */'@/views/blogHome/HomeArticle'),
      children: [
        {
          path: 'publish',
          name: 'publish',
          meta: { title: '文章发布' },
          component: () => import(/* webpackChunkName: "publish" */'@/views/blogHome/article/ArticlePublish')
        },
        {
          path: 'label',
          name: 'label',
          meta: { title: '标签管理' },
          component: () => import(/* webpackChunkName: "label" */'@/views/blogHome/article/ArticleLabel')
        },
        {
          path: 'list',
          name: 'list',
          meta: { title: '标签管理' },
          component: () => import(/* webpackChunkName: "list" */'@/views/blogHome/article/ArticleList')
        },
        {
          path: '/home/article',
          redirect: '/home/article/publish'
        }
      ]
    },
    {
      path: 'pages',
      name: 'pages',
      meta: { title: '界面管理' },
      component: () => import(/* webpackChunkName: "pages" */'@/views/blogHome/FrontPage'),
      children: [
        {
          path: 'fronthome',
          name: 'fronthome',
          meta: { title: '前台首页' },
          component: () => import(/* webpackChunkName: "fronthome" */'@/views/blogHome/fromt/FrontHome')
        },
        {
          path: 'frontcenter',
          name: 'frontcenter',
          meta: { title: '个人界面' },
          component: () => import(/* webpackChunkName: "frontcenter" */'@/views/blogHome/fromt/FrontCenter')
        },
        {
          path: 'message',
          name: 'message',
          meta: { title: '留言板' },
          component: () => import(/* webpackChunkName: "message" */'@/views/blogHome/fromt/FrontMessage')
        },
        {
          path: '/home/pages',
          redirect: '/home/pages/fronthome'
        }]
    },
    {
      path: 'talk',
      name: 'talk',
      meta: { title: '动态管理' },
      component: () => import(/* webpackChunkName: "talk" */'@/views/blogHome/HomeTalk'),
      children: [
        {
          path: 'pushtalk',
          name: 'pushtalk',
          meta: { title: '发布动态' },
          component: () => import(/* webpackChunkName: "pushtalk" */'@/views/blogHome/talk/PushTalk')
        },
        {
          path: 'talklist',
          name: 'talklist',
          meta: { title: '动态列表' },
          component: () => import(/* webpackChunkName: "talklist" */'@/views/blogHome/talk/TalkList')
        },
        {
          path: '/home/talk',
          redirect: '/home/pages/pushtalk'
        }]
    },
    {
      path: '/home',
      redirect: '/home/main'
    }
    ]
  },
  {
    path: '*',
    redirect: '/home'
    // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 定义全局拦截(守卫) 在跳转之前拦截
router.beforeEach((to, from, next) => {
  // 每次跳转都去发一次请求 login 页面除外方便拦截
  if (!(to.path.includes('login'))) {
    // 如果报错了就跳转页面
    axios.get('/').catch(res => {
      next('/login')
    }).then(res => next())
  } else {
    next()
  }
})
// 定义全局拦截（守卫）后置全局拦截
router.afterEach((to, from) => {
  // 每次切换的时候切换标题名字用到了路由元传参
  document.title = to.meta.title || '小赵后台'
})
export default router
